package com.nsn.sdm.clearcaseTool;

import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.nsn.vs.sdm.util.shell.UtilShell;

/**
 * before using this helper, you must set a clear case view first. 
 * @author cofan
 *
 */
public class ClearCaseHelper 
{
	
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(ClearCaseHelper.class);
	
	/**
	 * Error message when clear case error 
	 */
	private static final String ERROR_MESSAGE_ClEAR_CASE = "cleartool: Error:";
	
	/**
	 * the path of clearcase installing 
	 */
	private String clearCasePath;
	
	/**
	 * 
	 */
	private UtilShell session;
	
	/**
	 * Default constructor
	 */
	public ClearCaseHelper ()
	{
	}
	
	/**
	 * 
	 * @param host
	 * @param userName
	 * @param password
	 * @throws Exception
	 */
	public void connect (String host, String userName, String password) throws Exception
	{
		session = new UtilShell(host, userName, password);
		session.connect();
	}
	
	/**
	 * 
	 * @throws Exception
	 */
	public void disConnect() throws Exception
	{
		if (session != null && session.isConnected())
		{
			session.disConnect();
		}
	}
	
	/**
	 * 
	 * @param path
	 * @return boolean
	 * @throws Exception
	 */
	public boolean checkOut (String path) throws Exception
	{
		String output = session.executeCommand(this.clearCasePath + " co -nc " + path);
		return this.isOutputExpected(output, ERROR_MESSAGE_ClEAR_CASE);
	}
	/**
	 * 
	 * @param path
	 * @return boolean
	 * @throws Exception
	 */
	public boolean checkIn(String path)throws Exception
	{
		String output = session.executeCommand(this.clearCasePath + " ci -nc " + path);
		if (!this.isOutputExpected(output, ERROR_MESSAGE_ClEAR_CASE))
		{
			LOGGER.warn("check in the path: "+path+" faild");
			this.checkOutCancel(path);
			return false;
		}
		return true;
			
	}
	/**
	 * 
	 * @param path
	 * @throws Exception
	 */
	public void checkOutCancel(String path)throws Exception
	{
		session.executeCommand(this.clearCasePath + " ci -nc " + path);
	}
	/**
	 * replace label in file list
	 * @param file_List
	 * @param label
	 * @throws Exception
	 */
	public void tagLabel(List<String> file_List,String label)throws Exception
	{	
		for(String file : file_List)
			session.executeCommand(this.clearCasePath+" mklabel -nc -rep "+label+" "+file);
	}
	/**
	 * 
	 * @param label
	 * @throws Exception
	 */
	public void makeNewLabel(String label)throws Exception
	{
		session.executeCommand(this.clearCasePath+" mklbtype -nc "+label);
	}
	/**
	 * create a new element under the directory
	 * @param parentDir
	 * @param element
	 * @throws Exception
	 */
	public void addNewElement(String parentDir,String element)throws Exception
	{
		if (this.checkOut(parentDir))
		{
			session.executeCommand("cd "+parentDir+"; "+this.clearCasePath+" mkelem -nc -master" +element);
			this.checkIn(parentDir);
			if (!this.checkIn(parentDir))
				LOGGER.warn("creat the new element: "+element+" faild");
				
		}
		else
			this.checkOutCancel(parentDir);
	}
	
	/**
	 * 
	 * @param output
	 * @param expected
	 * @throws Exception
	 */
	private boolean isOutputExpected (final String output, final String error) throws Exception
	{
		if (output.contains(error))
		{
			throw new Exception ("Expected output not found: " + output);
		}
		return true;
	}
	
	public String getClearCasePath() 
	{
		return clearCasePath;
	}
	
	public void setClearCasePath(String clearCasePath) 
	{
		this.clearCasePath = clearCasePath;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception
	{
		BasicConfigurator.configure();
		PropertyConfigurator.configure("log4j.properties"); 
		
		ClearCaseHelper cc = new ClearCaseHelper();
		
		String host = "10.158.200.64";
		String userName = "cofan";
		String password = "Nokia123$";
		
		try
		{
			cc.connect(host, userName, password);
			cc.setClearCasePath("/opt/rational/clearcase/linux_x86/bin/cleartool");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			cc.disConnect();
		}
		
		// boolean flag=Test.checkout("/vobs/advantage/cafne3sappl/hlri_oam_cfg/adaptation/o2ml_com.nsn.nthlrfe-HI50.01.zip");
		// System.out.println(flag);
	}

}
